package com.colorful.insuranceproject.commonActivity;

import android.os.Bundle;
import android.os.Process;
import android.os.SystemClock;
import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.colorful.insuranceproject.R;
import com.colorful.insuranceproject.fragment.ServerFragment;
import com.colorful.insuranceproject.fragment.ConsultFragment;
import com.colorful.insuranceproject.fragment.HomeFragment;
import com.colorful.insuranceproject.fragment.MineFragment;
import com.lovely3x.common.activities.ActivityManager;
import com.lovely3x.common.activities.TabActivity;
import com.lovely3x.common.managements.user.UserManager;
import com.lovely3x.common.utils.blur.Blur;
import com.lovely3x.common.widgets.FragmentTabHost;
import com.lovely3x.loginandresgiter.bean.SimpleUser;

import butterknife.ButterKnife;

/**
 * Created by handsome-Bob on 2017/8/15.
 * <p>
 * 首页基础类
 */
public class MainActivity extends TabActivity {

    private long mPreviousClickedTime;

    private static final long DOUBLE_EXIT_INTERVAL = 500;

    private int[] btnRes;

    private String[] btnNames;

    /**
     * Fragment 列表
     */
    public static final Class[] FRAGMENTS = new Class[]{
            HomeFragment.class,
            ServerFragment.class,
            ConsultFragment.class,
            MineFragment.class
    };

    protected int getContentView() {
        return R.layout.activity_main;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Blur.cleanupCache(this);
        super.onCreate(savedInstanceState);
        setCurrentTab(tab);
    }

    @Override
    protected void initViews() {
        //关闭滑动返回手势
        setSwipeBackEnable(false);
        btnRes = new int[]{
                R.drawable.btn_home_selector,
                R.drawable.btn_server_selector,
                R.drawable.btn_consult_selector,
                R.drawable.btn_mine_selector};

        btnNames = getResources().getStringArray(R.array.home_names);
        super.initViews();
        setNeedLoginJudge(false);
    }

    @Override
    public FragmentTabHost getTabHost() {
        return ButterKnife.findById(this, android.R.id.tabhost);
    }

    @Override
    public int getTabContainer() {
        return android.R.id.tabcontent;
    }

    @Override
    public Class<? extends Fragment>[] getTabs() {
        return FRAGMENTS;
    }

    /**
     * 获取指定下标的视图
     *
     * @param index 下标
     * @return tab视图
     */
    @Override
    public View getTabItemView(int index) {
        View view = getLayoutInflater().inflate(R.layout.view_tab_item, null);
        ImageView icon = ButterKnife.findById(view, R.id.iv_view_tab_item);
        TextView text = ButterKnife.findById(view, R.id.tv_view_tab_item);
        icon.setImageResource(btnRes[index]);
        text.setText(btnNames[index]);
        return view;
    }

    @Override
    public void onBackPressed() {
        final long current = SystemClock.elapsedRealtime();
        final long interval = current - mPreviousClickedTime;
        this.mPreviousClickedTime = current;
        if (interval <= DOUBLE_EXIT_INTERVAL) {
            finish();
            ActivityManager.finishAll();
            System.exit(0);
            Process.killProcess(Process.myPid());
        } else {
            showToast(R.string.click_agin_to_exit);
        }
    }


    @Override
    protected void onResume() {
        super.onResume();
        SimpleUser sb = (SimpleUser) UserManager.getInstance().getCurrentUser();
        if (sb.getPayPwd().equals("")) {
            launchActivity(CheckingInformationActivity.class);
        }
    }
}
